<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use \app\common\Time;

class Cmd extends Command
{
  protected function configure()
  {
      // 指令配置
    $this->setName('order')
        ->addArgument('mothod', Argument::OPTIONAL, "your mothod")
        ->addOption('date', null, Option::VALUE_REQUIRED, '')
        ->setDescription('sync order');   
  }

  protected function execute(Input $input, Output $output)
  {
    if ($input->hasOption('date')) {
      $this->date = $input->getOption('date');
    } else {
      $this->date = '';
    }
    $mothod = trim($input->getArgument('mothod'));
    if (method_exists($this, $mothod)) {
      $this->$mothod();
    }else{
      $output->writeln("命令" . $mothod . '不存在' );
    }
  }
  //同步淘宝渠道备案信息
  public function publisher_ddx()
  {
    $union = getSettingType('饿了么');
    $waimai = new \app\common\Waimai();
    $r = $waimai->ddx_get('tbk/publisher_get',[
      'info_type'=>1,
      'relation_app'=>'common',
    ]);
    if ($r['code'] != 20) {
      return $r['msg'];
    }
    $rows = $r['data']['inviter_list']['map_data'];
    foreach ($rows as $k) {
      $ck = Db::name('user_relation')->where('relation_id',$k['relation_id'])->find();
      if (!$ck) {
        $v = array();
        $v['account_name'] = $k['account_name'];
        $v['create_date'] = $k['create_date'];
        if ($k['note']) {
          $uid = $k['note'];
          $v['note'] = $k['note'];
          $v['uid'] = $k['note'];
          Db::name('user')->where('id',$uid)->update([
            'relation_id'=>$k['relation_id'],
          ]);
        }
        $v['real_name'] = $k['real_name'];
        $v['relation_id'] = $k['relation_id'];
        Db::name('user_relation')->insert($v);
      }
    }
  }

  public function publisher()
  {
    $union = getSettingType('饿了么');
    $tb = new \app\common\TaobaoApi();
    $r = $tb->get('taobao.tbk.sc.publisher.info.get',[
      'info_type'=>1,
      'relation_id'=>$union['adzone_id'],
      'relation_app'=>'common',
    ]);
    print_r($r);
  }
}